﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace Tool.Helper
{
    public class LogHelper
    {
        private readonly static String LogPath = String.Format(@".\Log");
        private readonly static Object locker = new Object();
        private static void Write(string message,string path)
        {
            lock (locker)
            {
                try
                {
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    var LogFile = String.Format(@"{0}\{1}.log", path, DateTime.Now.Date.ToShortDateString().Replace("/", ""));
                    using (StreamWriter writer = new StreamWriter(LogFile, true, Encoding.Default))
                    {
                        writer.WriteLine("[{0}]   {1}", DateTime.Now, message);
                    }
                }catch (Exception) { }
            }
        }
        public static void LogInfo(string str)
        {
            var path = String.Format(@"{0}\{1}", LogPath, "Log");
            Write(str, path);
        }
        public static void LogError(string str)
        {
            var path = String.Format(@"{0}\{1}", LogPath, "Error");
            Write(str, path);
        }
        public static void LogError(Exception ex)
        {
            var str= JsonConvert.SerializeObject(ex);
            var path = String.Format(@"{0}\{1}", LogPath, "Error");
            Write(str, path);
        }
    }
}
